package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.mall.lib.domain.AuthUserResource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by caihongli on 2017/5/10.
 */
@Repository
public class AuthUserResourceRepositoryImpl implements AuthUserResourceRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(AuthUserResourceRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<AuthUserResource> findAuthUserResourceByMallIdAndUserId(Long mallId, Long userId) {
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append("select %s from auth_user_resource aur where aur.mall_id=:mallId");
            sql.append(" and aur.user_id=:userId");
            sql.append(" and aur.merchant_id is null");
            params.put("mallId", mallId);
            params.put("userId", userId);
            fields.append("aur.*");
        };
        return doing.go(AuthUserResource.class, objectMapper, jdbcTemplate, log);
    }

    /**
     * 查询拥有该链接权限的用户数据
     *
     * @param backUrl
     * @param frontendUrl
     * @return
     */
    @Override
    public List<AuthUserResource> findAuthUserResourceByUrl(String backUrl, String frontendUrl) {
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append("select %s from auth_user_resource aur where ");
            sql.append(" EXISTS(select 1 from auth_resource ar where aur.resource_id=ar.id ");
            if (StringUtils.isNotBlank(backUrl)) {
                sql.append(" and ar.backend_url like :backUrl");
                params.put("backUrl", "%" + backUrl + "%");
            }
            if (StringUtils.isNotBlank(frontendUrl)) {
                sql.append(" and ar.frontend_url like :frontendUrl");
                params.put("frontendUrl", "%" + frontendUrl + "%");
            }
            sql.append(" ) ");
            fields.append("aur.*");
        };
        return doing.go(AuthUserResource.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public List<Long> findAuthUserIdByUrl(String backUrl, String frontendUrl) {
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append("select %s from auth_user_resource aur where ");
            sql.append(" EXISTS(select 1 from auth_resource ar where aur.resource_id=ar.id ");
            if (StringUtils.isNotBlank(backUrl)) {
                sql.append(" and ar.backend_url like :backUrl");
                params.put("backUrl", "%" + backUrl + "%");
            }
            if (StringUtils.isNotBlank(frontendUrl)) {
                sql.append(" and ar.frontend_url like :frontendUrl");
                params.put("frontendUrl", "%" + frontendUrl + "%");
            }
            sql.append(" ) ");
            fields.append("distinct user_id ");
        };
        return doing.go(Long.class, objectMapper, jdbcTemplate, log);
    }
}
